﻿using System;
using System.Reflection;

namespace AMS.Inventory.Entities
{
    public class InventoryInfo:AbstractInventoryProperty
    {
        public static string STATE_LOST = "Lost", STATE_MATCH = "Match", STATE_WAIT = "Wait", STATE_UPDATED = "Updated", OBJ_ID="Id",BASE_OBJECT_ID="BaseObjectId";

        protected Guid m_Id;

        /// <summary>
        /// 数据唯一标识
        /// </summary>
        public virtual Guid Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        protected Guid m_BaseObjectId; 
        
        protected Guid m_PlanId;

        /// <summary>
        /// 对应台账的数据唯一标识
        /// </summary>
        public virtual Guid BaseObjectId
        {
            get { return m_BaseObjectId; }
            set { m_BaseObjectId = value; }
        }

        /// <summary>
        /// 盘点计划ID
        /// </summary>
        public virtual Guid PlanId
        {
            get { return m_PlanId; }
            set { m_PlanId = value; }
        }

        protected DateTime m_InventoryDate=new DateTime(1900,1,1);
        /// <summary>
        /// 盘点日期
        /// </summary>
        public virtual DateTime InventoryDate
        {
            get { return m_InventoryDate; }
            set { m_InventoryDate = value; }
        }

        protected DateTime m_CommintDate = new DateTime(1900, 1, 1);
        /// <summary>
        /// 上传日期
        /// </summary>
        public virtual DateTime CommintDate
        {
            get { return m_CommintDate; }
            set { m_CommintDate = value; }
        }

        protected string m_InventoryState;
        /// <summary>
        /// 盘点状态
        /// </summary>
        public virtual string InventoryState
        {
            get { return m_InventoryState; }
            set { m_InventoryState = value; }
        }

        protected string m_InventoryMemo;
        /// <summary>
        /// 盘点备注
        /// </summary>
        public virtual string InventoryMemo
        {
            get { return m_InventoryMemo; }
            set { m_InventoryMemo = value; }
        }

        protected string m_OldBarcode;
        /// <summary>
        /// 旧条码
        /// </summary>
        public virtual string OldBarcode
        {
            get { return m_OldBarcode; }
            set { m_OldBarcode = value; }
        }

        protected string m_InventoryUserInfo;
        /// <summary>
        /// 盘点人信息
        /// </summary>
        public virtual string InventoryUserInfo
        {
            get { return m_InventoryUserInfo; }
            set { m_InventoryUserInfo = value; }
        }


        /// <summary>
        /// 重写Equals方法
        /// </summary>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(obj, this))
                return true;
            if (obj == null)
                return false;
            var sObj = obj as InventoryInfo;
            return sObj != null && ((sObj.BaseObjectId == m_BaseObjectId && sObj.PlanId == m_PlanId) || sObj.Id == m_Id);
        }

        /// <summary>
        /// 重写GetHashCode方法
        /// </summary>
        public override int GetHashCode()
        {
            int hash = 199;
            hash = 15*hash*m_Id.GetHashCode();
            return hash;
        }

        public virtual void SetProByLedgerInfo(LedgerInfo obj)
        {
            m_InventoryState = STATE_WAIT;
            PropertyInfo[] pis = obj.GetType().GetProperties();
            if (pis == null || pis.Length == 0)
                return;
            foreach (PropertyInfo pi in pis)
            {
                string strIvProName = pi.Name;
                if (pi.Name == OBJ_ID)
                    strIvProName = BASE_OBJECT_ID;
                PropertyInfo ivPro = GetType().GetProperty(strIvProName);
                if (ivPro == null)
                    continue;
                if (!pi.PropertyType.Equals(ivPro.PropertyType))
                    continue;
                var val = pi.GetValue(obj, null);
                ivPro.SetValue(this, val, null);
            }
        }
    }
}